/*
 * Copyright (c) 2007-2008, Thomas Krause (tho.krause@gmail.com)
 * 
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 * 
 *     * Redistributions of source code must retain the above copyright notice,
 *       this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright notice,
 *       this list of conditions and the following disclaimer in the documentation
 *       and/or other materials provided with the distribution.
 *     * Neither the name of gidoo nor the names of its contributors may be used to
 *       endorse or promote products derived from this software without specific
 *       prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.gidoo.cms.data;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.Transient;
import javax.persistence.UniqueConstraint;
import org.gidoo.cms.GidooCMS;

/**
 * Represents a resource belonging to a folder.
 * @author <a href="mailto:tho.krause@gmail.com">Thomas Krause</a>
 */
@Entity
@Table(
  uniqueConstraints={
  @UniqueConstraint(columnNames={"folder_id", "name"})
  }
)
public class Resource implements Serializable
{
  
  private long id;
  
  private String name;
  private String type;
  private boolean isPublished;
  private RegisteredUser createdBy;
  private RegisteredUser publishedBy; 
  private Date creationTime;
  private Folder folder;
  //private byte[] content;
  
  public Resource()
  {
    
  }

  @Id
  @GeneratedValue
  public long getId()
  {
    return id;
  }

  public void setId(long id)
  {
    this.id = id;
  }

  /** Get the name */
  @Column(nullable=false)
  public String getName()
  {
    return name;
  }
  /** Set the name */
  public void setName(String name)
  {
    this.name = name;
  }

  /** Get the MIME-type as it will be sent to the browser (e.g img/png) */
  @Column(nullable=false)
  public String getType()
  {
    return type;
  }

  /** Set the MIME-type as it will be sent to the browser (e.g img/png) */
  public void setType(String type)
  {
    this.type = type;
  }

  /**
   * Get wether this resource is published and therefore can be shown to the user.
   */
  public boolean getIsPublished()
  {
    return isPublished;
  }

  /**
   * Set wether this resource is published and therefore can be shown to the user.
   */
  public void setIsPublished(boolean isPublished)
  {
    this.isPublished = isPublished;
  }

  /** Get the user who uploaded this file. */
  @ManyToOne
  public RegisteredUser getCreatedBy()
  {
    return createdBy;
  }

  /** @see #getCreatedBy() */
  public void setCreatedBy(RegisteredUser createdBy)
  {
    this.createdBy = createdBy;
  }

  /** Get the user who published this file */
  @ManyToOne
  public RegisteredUser getPublishedBy()
  {
    return publishedBy;
  }

  /** @see #getPublishedBy() */
  public void setPublishedBy(RegisteredUser publishedBy)
  {
    this.publishedBy = publishedBy;
  }

  /** Get the time when this resource was uploaded. */
  @Temporal(javax.persistence.TemporalType.DATE)
  public Date getCreationTime()
  {
    return creationTime;
  }

  /** @see #getCreationTime() */
  public void setCreationTime(Date creationTime)
  {
    this.creationTime = creationTime;
  }

  /** Get the folder this resource belongs to. */
  @ManyToOne(optional=false)
  @Column(nullable=false)
  public Folder getFolder()
  {
    return folder;
  }

  /** @see #getFolder() */
  public void setFolder(Folder folder)
  {
    this.folder = folder;
  }

  /**
   * Get the file which belongs to this resource. The data of resources is not
   * stored in the database but in the folder WEB-INF/files on the local file-system.
   * 
   * If you call this function and the file doesn't exist yet it will be created.
   * 
   * @param app The application (needed for getting the path)
   * @return The file. If it was not possible to create the file or we are not allowed
   *          to read or write to it this functions returns null.
   */
  public File getFile(GidooCMS app)
  {
    try
    {
      File file = new File(app.getRealPathToContext() + "/WEB-INF/files/" + this.getId());

      file.createNewFile();

      if (file.canRead() && file.canWrite())
      {
        return file;
      }
    }
    catch (IOException ex)
    {
      Logger.getLogger(Resource.class.getName()).log(Level.SEVERE, null, ex);
    }
    
    return null;
  }
  
//  /** The binary content of the resource. */
//  @Lob
//  public byte[] getContent()
//  {
//    return content;
//  }
//
//  /** @see {@link #getContent} */
//  public void setContent(byte[] content)
//  {
//    this.content = content;
//  }

  
  
  
}
